#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>

int main(int argc, char **argv)
{
        sqlite3 *db;
        char **dbResult;
        char *errmsg;
        int nRow, nColumn;
        int index=0;
        int i, j, rc;

        if( argc!=2 )
        {
                fprintf(stderr, "Usage: %s DATABASE \n", argv[0]);
                exit(1);
        }

        rc = sqlite3_open(argv[1], &db);
        if( rc != SQLITE_OK )
        {
                fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
                sqlite3_close(db);
                exit(1);
        }

        rc = sqlite3_get_table( db, "select * from film", &dbResult, &nRow, &nColumn, &errmsg);
        if (rc == SQLITE_OK)
        {
                printf("表格共%d 记录!\n", nRow);
                printf("表格共%d 列!\n", nColumn);
                // 前两个字段为字段名 field0, field1, row[0][0], row[0][1], row[1][0], row[1][1] ... ... ....
                // 是一维数组,不是二维数组,反正记着第0,第1列的值为字段名,然后才是字段值;
                printf( "字段名|字段值\n");
                printf( "%s | %s\n", dbResult[0], dbResult[1]);
                printf("--------------------------------\n");
                index = nColumn; //字段值从index开始呀
                for( i = 0; i < nRow ; i++ )
                {
                        for( j = 0 ; j < nColumn; j++ )
                        {
                                printf( "%-5s ",dbResult[index++]);
                        }
                        printf("\n");
                }
                printf("--------------------------------\n");
        }

        sqlite3_close(db);
        return 0;
}
